<?php

namespace App\Http\Controllers\Admin;

use App\Models\Role;
use App\Models\Auth;
use Illuminate\Validation\Rule;
use Illuminate\Http\Request;

class RoleController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // 获取搜索关键词
        $name = $request->get('name');
        //分页
        $data = Role::when($name, function ($query) use ($name){
            $query->where('name', 'like', "%{$name}%");
        })->paginate($this->pagesize);
        return view('admin.role.index', compact('data','name'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.role.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 异常处理
        try{
            $this->validate($request,[
                // 'name' => 'required|unique:roles,name',
                'name' => [
                    'required',
                    Rule::unique('roles')->where(function($query){  //软删除记录不参与唯一性验证
                        $query->where('deleted_at',null);
                    })
                ],
            ]);
        }catch (\Exception $e){
            return ['status'=>1000,'msg'=>'验证不通过'];
        }
        // 接受 all 所有数据
        Role::create($request->only('name'));
        return ['status'=>0,'msg'=>'添加角色成功'];
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(int $id)
    {
        //
        $model = Role::find($id);
        return view('admin.role.edit', compact('model'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, int $id)
    {
        //异常处理
        try {
            $this->validate($request,[
                // 'name'=>'required|unique:roles,name,' . $id . ',id'
                'name' => [
                    'required',
                    Rule::unique('roles','name')->where(function($query){
                        $query->where('deleted_at',null)->where('id','<>',request('id'));
                    })
                ],
            ]);
        } catch (\Exception $e){
            return ['status'=>1000, 'msg'=>'验证不通过!'];
        }
        // 修改角色入库
        Role::where('id',$id)->update($request->only(['name']));
        return ['status'=> 0, 'msg'=> '修改用户成功'];
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        Role::find($id)->delete();
        return ['status'=>0,'msg'=>'删除成功'];
    }

    // 给角色分配权限
    public function auth(Role $role) {
        // dump($role->nodes()->pluck('name','id')->toArray());  pluck取一列
        // 读取所有的权限
        $authAll = (new Auth())->allAuthList();
        // 读取当前角色所拥有的权限
        $authes = $role->authes()->pluck('id')->toArray();
        return view('admin.role.auth',compact('role','authes','authAll'));
    }
    //分配处理
    public function authSave(Request $request,Role $role){
        //关联模型的数据同步
        // sync作用：任何不在给定数组中的 IDs 将会从中介表中被删除。并且将新的权限ID写进关联表
        $role->authes()->sync($request->get('auth'));
        // return redirect()->route('admin.role.index');
        return ['status'=>0, 'msg'=>'编辑成功'];
    }
}
